/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iteracon.generator.visitor;

import iteracon.generator.RangeBase;
import iteracon.generator.Rng;
import iteracon.generator.iterator.FromToIter;
import iteracon.generator.iterator.FromToListIter;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 *range generator from one integer to another integer
 * @author martin
 */
public class FromTo extends RangeBase{
  private final int _from,
                    _to;
  /**
   * constructor
   * @param from as int
   * @param to as int
   */
  private FromTo(int from, int to){
    _from=from;
    _to=to;
  }
  
  /**
   * static factory
   * @param from as int
   * @param to as int
   * @return range generator as List
   */
  public static Rng make(int from, int to){
    return new FromTo(from,to);
  }

  @Override
  public int size() {
    return _to-_from;
  }

  @Override
  public Integer get(int index) {
    //if (index<size())
      return _from+index;
    //throw new IndexOutOfBoundsException();
  }

  @Override
  public Iterator<Integer> iterator() {
    return new FromToIter(_from,_to);
  }

  @Override
  public ListIterator<Integer> listIterator() {
    return new FromToListIter(_from, _to, 0);
  }

  @Override
  public ListIterator<Integer> listIterator(int index) {
    return new FromToListIter(_from, _to, index);
  }

  @Override
  public List<Integer> subList(int fromIndex, int toIndex) {
    if (fromIndex<0 || toIndex >= size())
      throw new IndexOutOfBoundsException();
    return FromTo.make(fromIndex, toIndex);
  }
   
  @Override
  public boolean contains(Object o) {
    //optimization
    if (!(o instanceof Integer))
      return false;
    return ((Integer)o) < _to && ((Integer)o) >= _from;
  }
  
  @Override
  public int indexOf(Object o) {
    //optimization
    if (!(o instanceof Integer) || ((Integer)o) >= _to || ((Integer)o) < _from)
      return -1;
    return ((Integer)o)-_from;
  }

  @Override
  public int lastIndexOf(Object o) {
    //optimization
    return indexOf(o);
  }

  @Override
  public int from() {
    return _from;
  }

  @Override
  public int to() {
    return _to;
  }

  @Override
  public int step() {
    return 1;
  }
}
